home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 1997
/
MacHack 1997.toast
/
Hacks
/
Hacks ’94
/
[√] Distribution Restricted!
/
Steve Sisak
/
TMFutures
/
UFailure.c
< prev
next >
Wrap
Text File
|
1994-06-26
|
7KB
|
288 lines
//----------------------------------------------------------------------------------------
//UFailure.cp
//Copyright © 1985-1993 Apple Computer, Inc. All rights reserved.
//----------------------------------------------------------------------------------------
#ifndef __UFAILURE__
#include "UFailure.h"
#endif
#ifndef __STDIO__
#include <stdio.h>
#endif
#ifndef __MEMORY__
#include <Memory.h>
#endif
#ifndef __RESOURCES__
#include <Resources.h>
#endif
#ifndef __ERRORS__
#include <Errors.h>
#endif
#ifndef qDebug
#define qDebug 0
#endif
FailInfoPtr gTopHandler = NULL;
//----------------------------------------------------------------------------------------
// Assertion:
//----------------------------------------------------------------------------------------
#pragma segment MAFailureRes
void Assertion(const Boolean condition, const StringPtr description)
{
if (!condition)
{
Failure(minErr, 0);
}
} // Assertion
//----------------------------------------------------------------------------------------
// FailMemError:
//----------------------------------------------------------------------------------------
#pragma segment MAFailureRes
void FailMemError()
{
OSErr e = MemError();
if (e != noErr)
Failure(e, 0);
} // FailMemError
//----------------------------------------------------------------------------------------
// FailNewMessage:
//----------------------------------------------------------------------------------------
#pragma segment MAFailureRes
void FailNewMessage(OSErr error,
long oldMessage,
long newMessage)
{
if (!oldMessage)
Failure(error, newMessage);
else
Failure(error, oldMessage);
} // FailNewMessage
//----------------------------------------------------------------------------------------
// FailNIL:
//----------------------------------------------------------------------------------------
#pragma segment MAFailureRes
void FailNIL(void* p)
{
if (!p)
Failure(memFullErr, 0);
} // FailNIL
//----------------------------------------------------------------------------------------
// FailNILResource:
//----------------------------------------------------------------------------------------
#pragma segment MAFailureRes
void FailNILResource(Handle r)
{
OSErr e;
if (!r)
{
e = ResError();
if (e == noErr)
e = resNotFound;
Failure(e, 0);
}
} // FailNILResource
//----------------------------------------------------------------------------------------
// FailOSErr:
//----------------------------------------------------------------------------------------
#pragma segment MAFailureRes
void FailOSErr(OSErr error)
{
if (error != noErr)
Failure(error, 0);
} // FailOSErr
//----------------------------------------------------------------------------------------
// ErrorIs:
// return true if error == expectedError
// false if error == noErr
// otherwise fail
//----------------------------------------------------------------------------------------
#pragma segment MAFailureRes
Boolean ErrorIs(OSErr expectedError, OSErr error)
{
if (error == noErr)
return true;
if (error != expectedError)
Failure(error, 0);
return true;
} // FailOSErr
//----------------------------------------------------------------------------------------
// FailResError:
//----------------------------------------------------------------------------------------
#pragma segment MAFailureRes
void FailResError()
{
OSErr e = ResError();
if (e != noErr)
Failure(e, 0);
} // FailResError
//----------------------------------------------------------------------------------------
// Failure:
//----------------------------------------------------------------------------------------
#pragma segment MAFailureRes
void Failure(OSErr error,
long message)
{
FailInfoPtr pf = gTopHandler;
if (pf)
{
// pop the stack first, because calling the handler is likely to result in a call
// to Failure
gTopHandler = pf->nextInfo;
#if 000
if (CanWriteLn() && ((error != noErr) || gIntenseDebugging))// only show 0 errors if _really_ looking
{
GetCallersMethodName(pWho);
fprintf(stderr, "Failure signaled by: %s\n", (char *) pWho);
fprintf(stderr, " error: %1d message: %1d (%1d/%1d)\n", error, message, message >> 16, message & 0x0000FFFF);
}
#endif
pf->error = error;
pf->message = message;
longjmp(pf->savedState,0); // Go execute the failure handler
}
else
{
if (qDebug)
ProgramBreak("\pFailure called, but no handler!");
// The following ExitToShell allows the application to fail somewhat gracefully
// in the case that an allocation request has failed during static initialization.
ExitToShell();
}
} // Failure
//----------------------------------------------------------------------------------------
// ProgramBreak:
//----------------------------------------------------------------------------------------
#pragma segment MAFailureRes
void ProgramBreak(const StringPtr grievance)
{
StringPtr report = grievance;
#if 0
ConfirmHighLevelDebugger();
if (gHighLevelDebuggerRunning)
SysBreakStr(report);
else
DebugStr(report);
#else
DebugStr(report);
#endif
} // ProgramBreak
//----------------------------------------------------------------------------------------
// ProgramReport:
//----------------------------------------------------------------------------------------
#pragma segment MAFailureRes
void ProgramReport(const StringPtr grievance,
const Boolean
#if qDebug
breakInDebugger
#endif
)
{
// CStr255 report = grievance + "\n";
StringPtr report = grievance;
#if 0
ConfirmHighLevelDebugger();
if (gHighLevelDebuggerRunning)
{
if (breakInDebugger)
SysBreakStr(report);
else
SysBreakFunc(report);
}
else
DebugStr(report);
#else
DebugStr(report);
#endif
} // ProgramReport
//----------------------------------------------------------------------------------------
// HandlerExists:
//----------------------------------------------------------------------------------------
#pragma segment MAFailureRes
Boolean HandlerExists(FailInfoPtr testFailInfoPtr)
{
FailInfoPtr pf = gTopHandler;
while (pf)
{
if (pf == testFailInfoPtr)
return true;
pf = pf->nextInfo;
}
return false;
} // HandlerExists
//----------------------------------------------------------------------------------------
// Success:
//----------------------------------------------------------------------------------------
#pragma segment MAFailureRes
void Success(FailInfo* fi)
{
#if 0
if (gTopHandler != &fi)
{
CStr255 msg;
fprintf(stderr, "gTopHandler: %p, parameter: %p\n", gTopHandler, &fi);
msg = "Problem with Success: ";
if (HandlerExists(&fi))
msg += "too few ";
else
msg += "too many ";
msg += "calls to Success";
ProgramBreak(msg);
}
fi.installed = false;
#endif
gTopHandler = fi->nextInfo;
} // Success